package com.travel.travelServer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travel.travelServer.entity.Apply;
import com.travel.travelServer.mapper.ApplyMapper;
import com.travel.travelServer.service.ApplyService;
import com.travel.travelServer.utils.BaseService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * (Apply)表服务实现类
 *
 * @author makejava
 * @since 2024-03-01 15:53:45
 */
@Service
public class ApplyServiceImpl extends BaseService<ApplyMapper, Apply> implements ApplyService {

    @Override
    public Page<Apply> page(Page<Apply> page, Map<String, Object> conditions) {
        LambdaQueryWrapper<Apply> applyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        applyLambdaQueryWrapper.like(Objects.nonNull(conditions.get("name")) && conditions.get("name") != "", Apply::getName, conditions.get("name"));
        return super.page(page,applyLambdaQueryWrapper);
    }

    @Override
    public Apply getById(Long id) {
        return super.getById(id);
    }

    @Override
    public Boolean add(Apply pojo) {
        Long userId = getCurrentUser().getId();
        if (Objects.isNull(userId)){
            return false;
        }
        LambdaQueryWrapper<Apply> applyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        applyLambdaQueryWrapper.eq(Apply::getApplyUserId, userId);
        Apply apply = super.getOne(applyLambdaQueryWrapper);
        if (Objects.nonNull(apply)){
            return false;
        }
        pojo.setApplyUserId(userId);
        return super.save(pojo);
    }

    @Override
    public Boolean update(Apply pojo) {
        return super.updateById(pojo);
    }

    @Override
    public Boolean removeOne(Long id) {
        return super.removeById(id);
    }

    @Override
    public Boolean batchRemove(List<Long> asList) {
        return super.removeByIds(asList);
    }

    @Override
    public Apply getByCurrent() {
        Long userId = getCurrentUser().getId();
        LambdaQueryWrapper<Apply> applyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        applyLambdaQueryWrapper.eq(Apply::getApplyUserId, userId);
        return super.getOne(applyLambdaQueryWrapper);
    }
}

